package cn.fangline.camerademo02;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by 抽离 on 2018/11/6.
 */
public class FileUtils {
	private static final String SD_PATH = "/sdcard/dskqxt/pic/";
	private static final String IN_PATH = "/dskqxt/pic/";

	/**
	 * 随机生产文件名
	 *
	 * @return
	 */
	private static String generateFileName() {
		return UUID.randomUUID().toString();
	}
	/**
	 * 保存bitmap到本地
	 *
	 * @param context
	 * @param mBitmap
	 * @return
	 */
	public static String saveBitmap(Context context, Bitmap mBitmap) {
		String savePath;
		File filePic;
		savePath = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/";
		try {
			filePic = new File(savePath + generateFileName() + ".jpg");
			if (!filePic.exists()) {
				filePic.getParentFile().mkdirs();
				filePic.createNewFile();
			}
			FileOutputStream fos = new FileOutputStream(filePic);
			mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
			fos.flush();
			fos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

		return filePic.getAbsolutePath();
	}

}
